package io.xccit.hotelsystem.ui.home;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.viewpager2.widget.ViewPager2;

import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import io.xccit.hotelsystem.R;
import io.xccit.hotelsystem.databinding.FragmentHomeBinding;
import io.xccit.hotelsystem.ui.home.adapter.BannerAdapter;
import io.xccit.hotelsystem.ui.home.adapter.HotelAdapter;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 首页
 */
public class HomeFragment extends Fragment {
    
    public static class Hotel {
        private int id;
        private String name;
        private String price;
        private int imageRes;
        private String desc;
        
        public Hotel(String name, String price, int imageRes) {
            this.name = name;
            this.price = price;
            this.imageRes = imageRes;
        }

        public Hotel(int id,String name, String price, int imageRes) {
            this.id = id;
            this.name = name;
            this.price = price;
            this.imageRes = imageRes;
        }

        public Hotel(int id,String name, String price, int imageRes, String desc) {
            this.id = id;
            this.name = name;
            this.price = price;
            this.imageRes = imageRes;
            this.desc = desc;
        }

        public Hotel(int imageRes,String name,String desc,String price){
            this.imageRes = imageRes;
            this.price = price;
            this.desc = desc;
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public String getPrice() {
            return price;
        }

        public int getImageRes() {
            return imageRes;
        }

        public String getDesc() {
            return desc;
        }

        public int getId() {
            return id;
        }

        public void setId(){
            this.id = id;
        }

        @Override
        public String toString() {
            return "Hotel{" +
                    "name='" + name + '\'' +
                    ", price=" + price +
                    ", imageRes=" + imageRes +
                    ", desc='" + desc + '\'' +
                    '}';
        }
    }

    private FragmentHomeBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        HomeViewModel homeViewModel =
                new ViewModelProvider(this, new ViewModelProvider.Factory() {
                    @NonNull
                    @Override
                    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
                        return (T) new HomeViewModel(requireContext());
                    }
                }).get(HomeViewModel.class);

        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        
        // 设置酒店列表适配器
        RecyclerView hotelRecyclerView = binding.featuredHotels;
        // 设置为竖向双列布局
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2, GridLayoutManager.VERTICAL, false);
        hotelRecyclerView.setLayoutManager(layoutManager);
        
        homeViewModel.getHotels().observe(getViewLifecycleOwner(), hotels -> {
            HotelAdapter hotelAdapter = new HotelAdapter(hotels);
            hotelRecyclerView.setAdapter(hotelAdapter);
        });
        
        // 初始化轮播图
        ViewPager2 viewPager = binding.viewPager;
        homeViewModel.getBannerImages().observe(getViewLifecycleOwner(), images -> {
            BannerAdapter adapter = new BannerAdapter(images, requireContext());
            viewPager.setAdapter(adapter);
        });
        
        // 设置轮播图自动切换
        final Handler handler = new Handler();
        final Runnable update = new Runnable() {
            @Override
            public void run() {
                if (viewPager.getCurrentItem() == homeViewModel.getBannerImages().getValue().size() - 1) {
                    viewPager.setCurrentItem(0);
                } else {
                    viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
                }
            }
        };
        
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(update);
            }
        }, 2000, 2000);
        
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}